﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TravelSite.Entities;
using Dapper;

namespace TravelSite.DAL.Repositories
{
    public class TourRepository : BaseRepository<Tour>
    {
        protected override string TableName
        {
            get { return "[Tour]"; }
        }

        public override IEnumerable<Tour> FindAll()
        {
            string strSQL = @"  select vo.TourId, vo.TourName,
                    convert(bit, case 
                                    when exists(select 1 from Contact (nolock) as c where c.TourHotelId = vo.TourId) then 0
                                else 1 end) as CanDelete
                    from [Tour] (nolock) as vo 
                    order by vo.TourName ";

            IEnumerable<Tour> result = Connection.Query<Tour>(strSQL);
            return result;
        }

        public override Tour Find(int id)
        {
            Tour result = Connection.Query<Tour>(
                @"  select vo.TourId, vo.TourName
                    from [Tour] (nolock) as vo
                    where vo.TourId = @TourId", new { TourId = id }).SingleOrDefault();
            return result;
        }

        public override void Insert(Tour item)
        {
            Connection.Execute(
                @"insert into [Tour] 
                (TourName)
                values (@TourName)", item);
        }

        public override void Update(Tour item)
        {
            Connection.Execute(
                @"update Tour
                    set TourName = @TourName
                    where (TourId = @TourId)", item);
        }

        public override void Delete(int id)
        {
            Connection.Execute(
                @"delete 
                    from Tour
                    where (TourId = @TourId)", new { TourId = id });
        }

    }
}
